package controller;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import dataClass.Kullanicilar;
import model.ConnectDB;


public class LoginController {
	
	Kullanicilar aktifKullanici = null;
	public LoginController(){
		
	}
	
	public boolean loginAttempt(String _userName, char[] _password){
		ArrayList<String> user;
		Connection con = null;
		boolean success = false;
		
		try{
			String query = "SELECT kullanici_id,kullanici_tipi_id,kullanici_adi " +
					"FROM Kullanicilar " +
					"WHERE kullanici_adi='"+_userName+"' "+
					"AND kullanici_parolasi="+String.valueOf(_password)+"";
		
			con	  = ConnectDB.getConnection();
			user = ConnectDB.getDistinctData(query, con, 3);
			if(!user.isEmpty()){
				aktifKullanici = new Kullanicilar();
				aktifKullanici.setKullanici_ID(Integer.parseInt(user.get(0)));
				aktifKullanici.setKullanici_tipi_ID(Integer.parseInt(user.get(1)));
				aktifKullanici.setKullanici_adi(user.get(2));
				success = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		finally{
			if(con!=null){
				try{
					con.close();
				}catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
		return success;
	}
	
	public Kullanicilar getAktifKullanici()
	{
		return this.aktifKullanici;
	}
}
